# while循环
"""
基本格式：
        while 条件:
                循环体

        1. 条件为真，则执行循环体代码
        2. 条件为假，则结束循环

必要的循环条件:
        1. 循环变量的初始值
        2. 循环的判定条件
        3. 循环变量的更新语句

"""

# 例1. 打印1~10
# num = 1
# while num <= 10:
#     print(num)
#     num += 1

# 例2. 计算1~100 的值
# num = 1
# sum = 0
# while num <= 100:
#     sum = sum + num
#     num += 1
# print(sum)  # 不能和上面的代码并排，不然会以为是一个循环的循环体代码


# 例3. 计算5的阶乘
# num = 1
# result = 1
# while num <= 5:
#     result *= num
#     num += 1
# print(f'result = {result}')

# 例4. 求1！+2！+3！+4！+5！的和
# num = 1
# sum = 0
# while num <= 5:
#     # 先计算当前 num！是多少，再往sum上进行累加
#     factorResult = 1
#     i = 1
#     while i <= num:
#         factorResult *= i
#         i += 1
#     sum += factorResult
#     num += 1
# print(f'sum = {sum}')
